package jwdt.xadmin.work.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TDoc;
import jwdt.xadmin.work.service.DocManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;
import com.ezerg.web.ext.FileUploadAware;
import com.ezerg.web.ext.HttpServletRequestAware;
import com.ezerg.web.ext.HttpServletResponseAware;
import com.ezerg.web.ext.ServletContextAware;
import com.ezerg.web.fileupload.FileDownload;
import com.ezerg.web.fileupload.FileItem;

@Controller("/xadmin/work")
public class DocManageAction extends BaseAction implements FileUploadAware, HttpServletRequestAware, HttpServletResponseAware, ServletContextAware{

	private DocManageService docManageService = (DocManageService)app.getBean("docManageService");
	
	private HttpServletResponse response = null;
	
	private ServletContext servletContext = null;
	
	@RequestMapping("/listDoc")
	public Page doListImage(Form f, Module m) throws Exception {
		TUser user = (TUser) f.getSession("userInfo");
		TDoc obj = (TDoc)f.copy2Class(TDoc.class);
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		return m.getWritePage(f, docManageService.doList(pu,user,obj).toJson());
	}
	
	@RequestMapping("/addDoc")
	public Page doAddact(Form f, Module m) throws Exception {
		TDoc obj = (TDoc)f.copy2Class(TDoc.class);
		FileItem file = f.getFileItem("file");
		TUser user = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, docManageService.doAddact(obj,file,user));
	}
	
	@RequestMapping("/editDoc")
	public Page doEditact(Form f, Module m) throws Exception {
		int id = f.getInt("docid");
		TDoc obj = (TDoc)f.copy2Class(TDoc.class);
		FileItem file = f.getFileItem("file");
		//System.out.println("file="+file);
		TUser user = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, docManageService.doEditact(obj,id,file,user));
	}
	
	@RequestMapping("/delDoc")
	public Page doDel(Form f, Module m) throws Exception {
		int id = f.getInt("docid");
		return m.getWritePage(f, docManageService.doDelact(id));
	}
	
	@RequestMapping("/shenheDoc")
	public Page doShenhe(Form f, Module m) throws Exception {
		int id = f.getInt("docid");
		String result = f.getString("result");
		return m.getWritePage(f, docManageService.doUpdateState(id, result));
	}
	
	@RequestMapping("/docWindow")
	public Page doLoad(Form f, Module m) throws Exception {
		int id = f.getInt("docid");
		f.add("obj", docManageService.doLoad(id));
		return m.findPage("/xadmin/work/docWindow");
	}
	
	@RequestMapping("/docDownLoad")
	public Page doDownLoad(Form f, Module m) throws Exception {
		int id = f.getInt("docid");
		TDoc doc = (TDoc) Jdb.get().get(TDoc.class, id);
		FileDownload down = new FileDownload(this.servletContext, this.response);
		try {
			down.downloadFile(doc.getContent());
		} catch(Exception e) {
			f.add("ret", "您下载的文件不存在，请与管理员联系。").add("closed",	 "true");
			return m.findPage("alert");
		}
		return null;
	}
	
	public String getAllowedFileExt() {
		return "zip,doc,txt,docx,rar";
	}

	public int getMaxUploadFileSize() {
		return 1024000;
	}

	public int getUploadSizeThreshold() {
		return 512;
	}

	@Override
	public void setHttpServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setHttpServletRequest(HttpServletRequest request) {
		
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
}
